Skip to content

Define arbitrary clipping regions#300

Open
oznogon wants to merge 4 commits intodaid:masterfrom
oznogon:generic-scrolling-guicontainer
Open

Define arbitrary clipping regions#300
oznogon wants to merge 4 commits intodaid:masterfrom
oznogon:generic-scrolling-guicontainer

Conversation

@oznogon
Copy link
Copy Markdown
Contributor

@oznogon oznogon commented Mar 9, 2026

Implement GL_SCISSOR_TEST at the RenderTarget level to support arbitrary clipping of rendered areas. Add clip_region_stack vector, push/popClipRegion() to define and access visible areas as intersected sp::Rects.

These primarily support the implementation of nested GUI elements that can be visibly clipped within and scrolled by a parent container.

This allows the definition of a GUI container that renders and interacts with only what is within a clip region, while allowing overflow content to be scrolled by repositioning other elements into the clip region.

For example, given:

void GuiScrollContainer::drawElements(
    glm::vec2 mouse_position,
sp::Rect /* parent_rect */,
sp::RenderTarget& renderer
)
{
    sp::Rect content_rect = getContentRect();

    renderer.pushClipRegion(content_rect);

    for (auto it = children.begin(); it != children.end(); )
    {

    ... pass translated mouse events through to scrolled
    contents ...

}

    renderer.popClipRegion();
}

Rendering of all child elements of this container is clipped to the parent's content_rect dimensions and shifted vertically by the scroll_offset via overridden GuiContainer::updateLayout(). Mouse events (clicks, hover, focus changes) are also passed through to the repositioned child elements.

Implement GL_SCISSOR_TEST at the RenderTarget level to support
arbitrary clipping of rendered areas, and implement translation
functions to support scrolling of arbitrary rendered areas. These
primarily support the implementation of nested GUI elements that
can be visibly clipped within and scrollable by a parent container.

- Add scissor_stack vector, push/popScissorRect() to define and
  access visible areas defined as sp::Rects.
- Add translation_stack vector, push/popTranslation() to define and
  access positional deltas, translate them to pixel coordinates,
  and track the render's total translation value.
- Add getTranslation() to expose the total current translation
  value, to allow for relative positioning to a translated render.
- Add static applyProjectionMatrix() to apply a translation to a
  2D projection matrix, for mapping a virtual coordinate to a
  relative screen position.

This allows the definition of a GUI container that renders and
interacts with only what is within a scissor rect, while allowing
overflow content to be scrolled (translated) into that rect.

For example, given:

    void GuiScrollContainer::drawElements(
        glm::vec2 mouse_position,
	sp::Rect /* parent_rect */,
	sp::RenderTarget& renderer
    )
    {
        sp::Rect content_rect = getContentRect();

        renderer.pushScissorRect(content_rect);
        renderer.pushTranslation({0.0f, -scroll_offset});

        glm::vec2 layout_mouse = mouse_position + glm::vec2{
	    0.0f, scroll_offset
	};

        for (auto it = children.begin(); it != children.end(); )
        {

	    ... pass translated mouse events through to scrolled
	    contents ...

	}

        renderer.popTranslation();
        renderer.popScissorRect();
    }

Rendering of all child elements of this container is clipped to
the parent's content_rect dimensions and shifted vertically by the
translation offset. Mouse events are also passed through to the
translated positions of the child elements.

// Stack nested translation vectors to determine their offsets relative to
// the container and each other.
void pushTranslation(glm::vec2 virtual_offset);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I like these. Feels like a recipe for confusion in combination with the layout managers of the GUI system.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed them and implemented scroll_offset in GuiContainer/GuiScrollContainer to be applied in updateLayout(). So clipping remains part of RenderTarget and scrolling is now part of the layout system.

@oznogon oznogon changed the title Define arbitrary clipping regions, translate renders Define arbitrary clipping regions Mar 22, 2026
@oznogon oznogon force-pushed the generic-scrolling-guicontainer branch from d5dd0b8 to c121e94 Compare March 24, 2026 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants